package com.edu.dao.impl;

import com.edu.dao.BaseDao;
import com.edu.dao.VehiclesDao;
import com.edu.entity.Vehicles;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VehiclesDaoImpl extends BaseDao implements VehiclesDao {
    @Override
    public List<Vehicles> queryByType(String type, int page, int pageSize) {
        page = (page - 1) * pageSize;
        String sql = "SELECT * from vehicles WHERE type = '" + type + "' AND is_rented = 0 LIMIT " + page + "," + pageSize + "";
        System.out.println(sql);
        ResultSet resultSet = super.queryResultSet(sql);
        List<Vehicles> list = new ArrayList<Vehicles>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String typen = resultSet.getString("type");
                String description = resultSet.getString("description");
                double price = resultSet.getDouble("price");
                String imgePath = resultSet.getString("image_path");
                int isRented = resultSet.getInt("is_rented");
                Vehicles vehicles = new Vehicles(id, name, typen, description, price, imgePath, isRented);
                list.add(vehicles);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, ps, resultSet);
        }
        return list;
    }

    @Override
    public int queryByTypeCount(String type) {
        String sql = "SELECT count(1) 'num'  from vehicles WHERE type = '" + type + "' AND is_rented = 0";
        System.out.println(sql);
        ResultSet resultSet = super.queryResultSet(sql);
        List<Vehicles> list = new ArrayList<Vehicles>();
        int id = 0;
        try {
            while (resultSet.next()) {
                id = resultSet.getInt("num");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, ps, resultSet);
        }
        return id;
    }

    @Override
    public Vehicles queryByTId(int id) {
        String sql = "SELECT * from vehicles WHERE id = " + id;
        System.out.println(sql);
        ResultSet resultSet = super.queryResultSet(sql);
        Vehicles vehicles = null;
        try {
            while (resultSet.next()) {
                int id1 = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String typen = resultSet.getString("type");
                String description = resultSet.getString("description");
                double price = resultSet.getDouble("price");
                String imgePath = resultSet.getString("image_path");
                int isRented = resultSet.getInt("is_rented");
                 vehicles = new Vehicles(id1, name, typen, description, price, imgePath, isRented);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, ps, resultSet);
        }
        return vehicles;
    }
}
